package edu.webtech.project.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.webtech.project.client.FileService;
import edu.webtech.project.client.FileServiceAsync;
import edu.webtech.project.shared.FileDataDTO;

public class SearchPanel extends Composite {

	private static SearchPanelUiBinder uiBinder = GWT
			.create(SearchPanelUiBinder.class);
	
	FileServiceAsync fileService = GWT.create(FileService.class);
	
	@UiField VerticalPanel searchPanel;
	@UiField TextBox       txtSearchBox;
	@UiField Button        btnSearch;
	@UiField ListBox       listSemesterBox;
	@UiField ListBox       listCourseName;
	@UiField FlexTable     flexFilesTable;
	@UiField SearchPanelStyle style;
	
	private String strFileName;
	private String strSemesterName;
	private String strCourseName;
	
	

	interface SearchPanelUiBinder extends UiBinder<Widget, SearchPanel> {
	}
	
	interface SearchPanelStyle extends CssResource {
		// As the CSS resource has a dash (-) in it
		// otherwise use the same name
		// @CssResource.ClassName("class-name")
		// String loginhead();

		// using the same name as in the CSS style in XML file
		String profilepageheader();
	}

	public SearchPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
		populateSemesterName();
		populateCourseName();
		DisplayTable();
	}
	
	
	

	public void populateSemesterName() {
		String[] names = { "Select semester", "SS2009", "WS2009", "SS2010",
				"WS2010", "SS2011", "WS2011", "SS2012", "WS2012", "SS2013",
				"WS2013" };
		for (String name : names) {
			listSemesterBox.addItem(name);
		}

	}

	public void populateCourseName() {
		String[] names = { "Select course", "Medicine", "Informatics",
				"Electrical Eng.", "Communication Eng." };
		for (String name : names) {
			listCourseName.addItem(name);
		}
	}

	public void DisplayTable() {

		flexFilesTable.removeAllRows();
		insertTableHeader();

		fileService.getAllFiles(new AsyncCallback<List<FileDataDTO>>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(List<FileDataDTO> result) {

				if (0 == result.size()) {
					flexFilesTable.getFlexCellFormatter().setColSpan(1, 0, 3);
					flexFilesTable.setText(1, 0, "No file/s found on the server.");
					return;
				}
				// insert rows with the received results
				inserTableRows(result);
			}
		});

	}
	
	public void SearchTable() {

		flexFilesTable.removeAllRows();
		insertTableHeader();

		getFormData(); // Update fields from the GUI to variables.
		fileService.searchFiles(strFileName, strSemesterName, strCourseName, 
				new AsyncCallback<List<FileDataDTO>>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(List<FileDataDTO> result) {

				if (0 == result.size()) {
					flexFilesTable.getFlexCellFormatter().setColSpan(1, 0, 3);
					flexFilesTable.setText(1, 0, "No file/s found on the server.");
					return;
				}
				// insert rows with the received results
				inserTableRows(result);
			}
		});
	}
	
	public void getFormData() {
		this.strFileName     = txtSearchBox.getText().trim();
		this.strSemesterName = listSemesterBox.getItemText(listSemesterBox.getSelectedIndex());
		this.strCourseName   = listCourseName.getItemText(listCourseName.getSelectedIndex());
	}
	
	public void insertTableHeader() {
		flexFilesTable.setText(0, 0, "Filename");
		flexFilesTable.setText(0, 1, "Course");
		flexFilesTable.setText(0, 2, "Semester");
		flexFilesTable.setText(0, 3, "Subject");
		flexFilesTable.setText(0, 4, "Link");
		flexFilesTable.getRowFormatter().addStyleName(0, style.profilepageheader());
	}
	
	public void inserTableRows(List<FileDataDTO> result) {
		for (int i = 0; i < result.size(); i++) {
			flexFilesTable.setText(i + 1, 0, result.get(i).getFileName());
			flexFilesTable.setText(i + 1, 1, result.get(i).getSemesterName());
			flexFilesTable.setText(i + 1, 2, result.get(i).getCourseName());
			flexFilesTable.setText(i + 1, 3, result.get(i).getSubjectName());
			flexFilesTable.setWidget(i + 1, 4, new Anchor("Download", false, result.get(i).getUrl(), "_blank" ));					
		}
	}
	
	
	@UiHandler("btnSearch")
	void onButtonSearchClick(ClickEvent e) {
		SearchTable();
	}
	
	@UiHandler("txtSearchBox")
	void onSearchBoxKeyDown(KeyDownEvent e) {
		
		switch (e.getNativeEvent().getKeyCode()) {
		
		case KeyCodes.KEY_ENTER:
			SearchTable();
			break;
		}
	}
	
	@UiHandler("listSemesterBox")
	void onSemesterSelection(ChangeEvent e) {
		SearchTable();
	}
	
	@UiHandler("listCourseName")
	void onCourseSelection(ChangeEvent e) {
		SearchTable();
	}
	

}
